(function (exports) {
    var appId = null;
    exports.WXUtils = {
        isWXAppInstalled: function () {
            if (!cc.sys.isNative)
                return false;
            return (cc.sys.os == cc.sys.OS_IOS
                ? jsb.reflection.callStaticMethod("WXUtil", "isWXAppInstalled")
                : jsb.reflection.callStaticMethod(packageUri + "/utils/WeixinUtil", "isWXAppInstalled", "()I"));
        },
        getAppid: function () {
            if (appId)
                return appId;
            return appId = (cc.sys.os == cc.sys.OS_IOS
                ? jsb.reflection.callStaticMethod("WXUtil", "getWeixinAppId")
                : jsb.reflection.callStaticMethod(packageUri + '/Global', "getWeixinAppId", "()Ljava/lang/String;"));
        },
        redirectToWeixinLogin: function () {
            if (cc.sys.os == cc.sys.OS_IOS)
                jsb.reflection.callStaticMethod("WXUtil", "redirectToWeixinLogin");
            else
                jsb.reflection.callStaticMethod(packageUri + "/utils/WeixinUtil", "redirectToWeixinLogin", "()V");
        },
        getWXLoginCode: function () {
            return appId = (cc.sys.os == cc.sys.OS_IOS
                ? jsb.reflection.callStaticMethod("WXUtil", "getWXLoginCode")
                : jsb.reflection.callStaticMethod(packageUri + "/utils/WeixinUtil", "getWXLoginCode", "()Ljava/lang/String;"));
        },
        setWXLoginCode: function (code) {
            return appId = (cc.sys.os == cc.sys.OS_IOS
                ? jsb.reflection.callStaticMethod("WXUtil", "setWXLoginCode:", code)
                : jsb.reflection.callStaticMethod(packageUri + "/utils/WeixinUtil", "setWXLoginCode", "(Ljava/lang/String;)V", code));
        },
        captureAndShareToWX: function (node, depthStencilFormat) {
            if(getNativeVersion()<='1.4.0'){
                captureAndShareToWX(node, depthStencilFormat);
                return;
            }

            var winSize = cc.director.getWinSize();
            var texture = new cc.RenderTexture(winSize.width, winSize.height, null, depthStencilFormat);
            if (!texture)
                return;

            texture.retain();

            texture.setAnchorPoint(0, 0);
            texture.begin();
            node.visit();
            texture.end();

            if(getNativeVersion()<"3.0.0"){
                WXUtils.shareTexture(texture);
            }else {
                var layer = new TishiShareLayer(texture);
                cc.director.getRunningScene().addChild(layer, 100);
            }


        },
        shareTexture:function (texture) {
            var time = timestamp2time(Math.round((new Date()).valueOf() / 1000));
            time = time.replace(/[\s:-]+/g, '_');
            var nameJPG = "ss-" + time + ".jpg";
            if (cc.sys.os == cc.sys.OS_ANDROID) {
                texture.saveToFile(nameJPG, cc.IMAGE_FORMAT_JPEG, false, function (renderTexture, str) {
                    texture.release();
                    jsb.reflection.callStaticMethod(
                        packageUri + "/utils/WeixinUtil",
                        "sharePic",
                        "(Ljava/lang/String;Z)V",
                        nameJPG,
                        false
                    );
                });
            }
            else if (cc.sys.os == cc.sys.OS_IOS) {
                texture.saveToFile(nameJPG, cc.IMAGE_FORMAT_JPEG, true, function (renderTexture, str) {
                    texture.release();
                    jsb.reflection.callStaticMethod(
                        "WXUtil",
                        "sharePic:imageName:sceneType:",
                        jsb.fileUtils.getWritablePath(),
                        nameJPG,
                        0
                    );
                });
            }
        },
        shareText: function (text, sceneType, transaction) {
            if (!cc.sys.isNative) {
                return;
            }
            if (cc.sys.os == cc.sys.OS_IOS) {
                jsb.reflection.callStaticMethod(
                    "WXUtil",
                    "shareText:sceneType:",
                    text,
                    (sceneType ? 1 : 0)
                );
            }
            else {
                jsb.reflection.callStaticMethod(
                    packageUri + "/utils/WeixinUtil",
                    "shareText",
                    "(Ljava/lang/String;ZLjava/lang/String;)V",
                    text,
                    (sceneType ? true : false),
                    transaction
                );
            }
        },
        shareUrl: function (url, title, description, sceneType, transaction) {
            if (cc.sys.os == cc.sys.OS_IOS) {
                jsb.reflection.callStaticMethod(
                    "WXUtil",
                    "shareUrl:title:description:sceneType:",
                    url,
                    title,
                    description,
                    (sceneType ? 1 : 0)
                );
            }
            else {
                jsb.reflection.callStaticMethod(
                    packageUri + "/utils/WeixinUtil",
                    "shareUrl",
                    "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLjava/lang/String;)V",
                    url,
                    title,
                    description,
                    (!!sceneType),
                    transaction
                );
            }
        },
        beginWxPay: function () {
            var that = this;
            var data = {
                appid: WXA[gameData.appId],
                player: gameData.uid,
                area: "hn",
                cid: 6,
                timestamp: getCurTimestamp()
            };
            data.sign = Crypto.MD5("" + data.appid + data.area + data.cid + data.player + data.timestamp);
            NetUtils.httpPost("http://pay.yayayouxi.com/payServer/game/wechatOrder", data,
                function (data) {
                    if (data["prepay_id"]) {
                        that.wxPay(data["prepay_id"]);
                    } else {
                        alert1("支付请求失败,请检查网络");
                    }
                },
                function () {
                    alert1("支付请求失败,请检查网络");
                }
            );
        },
        wxPay: function (prepayId) {
            var nonceStr = RandomString(16);
            var packageValue = "Sign=WXPay";
            var timeStamp = getCurTimestamp();
            if (cc.sys.os == cc.sys.OS_IOS) {
                var str = "appid=" + WXA[gameData.appId] + "&noncestr=" + nonceStr + "&package=" + packageValue + "&partnerid=" + WXP[gameData.appId] + "&prepayid=" + prepayId + "&timestamp=" + timeStamp + "&key=" + WXK[gameData.appId];
                var ios_sign = Crypto.MD5(str).toUpperCase();
                jsb.reflection.callStaticMethod(
                    "AppController",
                    "wxPay:prepayId:package:nonceStr:timeStamp:sign:",
                    WXP[gameData.appId],
                    prepayId,
                    packageValue,
                    nonceStr,
                    timeStamp + "",
                    ios_sign
                );
            } else if (cc.sys.os == cc.sys.OS_ANDROID) {
                var android_sign = Crypto.MD5("appId=" + gameData.appId + "&nonceStr=" + nonceStr + "&package=" + packageValue + "&partnerId=" + WXP[gameData.appId] + "&prepayId=" + prepayId + "&timeStamp=" + timeStamp).toUpperCase();
                jsb.reflection.callStaticMethod(
                    "org/cocos2dx/javascript/AppActivity",
                    "wxPay",
                    "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
                    gameData.appId,
                    WXP[gameData.appId],
                    prepayId,
                    packageValue,
                    nonceStr,
                    timeStamp,
                    android_sign
                );
            }
        }
    };

})(window);